﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Globalization;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.UI.Modules;
using DotNetNuke.Services.Installer.Packages;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Game
{
    public partial class GameEdit : CGOBase
    {        
        private int gameID = Null.NullInteger;

        private string CultureCode
        {
            get { return LocaleController.Instance.GetCurrentLocale(PortalId).Code; }
        }

        public int GameID
        {
            get
            {
                if (gameID == 0)
                {
                    if ((Request.QueryString["GameID"] != null))
                    {
                        gameID = Int32.Parse(Request.QueryString["GameID"]);
                    }
                }
                return gameID;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                BindData();
            }

        }

        public void BindData(int intGameID)
        {
            gameID = intGameID;
        }

        private void BindData()
        {
            if (GameID>0)
            {
                GameController ctlGame = new GameController();
                GameInfo objGame = ctlGame.GetGame(GameID);
                if (!Null.IsNull(objGame))
                {
                    pnlGameEdit.Visible = true;
                    lblMessage.Visible = false;
                    txtGameName.Text = objGame.Name;
                    txtDescription.Text = objGame.Description;

                    List<TabInfo> listTabs = TabController.GetPortalTabs(TabController.GetTabsBySortOrder(PortalId, CultureCode, true), Null.NullInteger, true, "<" + Localization.GetString("None_Specified") + ">", true, false, false, false, false);
                    ddlGameTabID.DataSource = listTabs;
                    ddlGameTabID.DataBind();

                    if (objGame.GameTabID > 0)
                    {
                        ListItem selItem = ddlGameTabID.Items.FindByValue(objGame.GameTabID.ToString());
                        if (selItem != null)
                            selItem.Selected = true;
                    }

                    ctlImageURL.Url = objGame.ImageURL;
                    ctlImageURL.UrlType = objGame.ImageType;
                    txtImageWidth.Text = objGame.ImageWidth.ToString();
                    txtImageHeight.Text = objGame.ImageHeight.ToString();
                }
                else
                {
                    //Response.Redirect(Globals.NavigateURL(), true);
                    pnlGameEdit.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = Localization.GetString("ErrorNoData", LocalResourceFile);
                }
            } 
        }

        public void UpdateData(int intGameID)
        {
            gameID = intGameID;
            UpdateData();
        }

        private void UpdateData()
        {
            try
            {
                if (GameID>0)
                {
                    GameController ctlGame = new GameController();
                    GameInfo objGame = new GameInfo();
                    objGame.GameID = GameID;
                    objGame.PortalID = PortalId;
                    objGame.Name = txtGameName.Text;
                    objGame.Description = txtDescription.Text;

                    objGame.ImageURL = ctlImageURL.Url;
                    objGame.ImageType = ctlImageURL.UrlType;
                    if (string.IsNullOrEmpty(txtImageWidth.Text))
                        objGame.ImageWidth = 0;
                    else
                        objGame.ImageWidth = Int32.Parse(txtImageWidth.Text);
                    if (string.IsNullOrEmpty(txtImageHeight.Text))
                        objGame.ImageHeight = 0;
                    else
                        objGame.ImageHeight = Int32.Parse(txtImageHeight.Text);
                    objGame.ImageDisplay = chkImageDisplay.Checked;

                    objGame.GameTabID = Int32.Parse(ddlGameTabID.SelectedValue);

                    ctlGame.SaveGame(objGame);
                }
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

    }
}